package org.hxyjs.service.impl;


import org.hxyjs.dao.AdminDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Account;
import org.hxyjs.entity.Admin;
import org.hxyjs.page.PageInfo;

import org.hxyjs.service.AccountService;
import org.hxyjs.service.AdminService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("adminService")
public class AdminServiceImpl implements AdminService, AccountService {
    @Resource
    AdminDao adminDao;
    @Override
    public DTO<Admin> getAllAdmins()  throws Exception{
        List<Admin> admins = adminDao.getAllAdmins();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(admins).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Admin>(messageEnum.getCode(),messageEnum.getMessage(),admins);
    }

    @Override
    public DTO<Admin> deleteAdminByAno(String ano)  throws Exception{
        Integer result = adminDao.deleteAdminByAno(ano);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEADMINSUCCESS:MessageEnum.DELETEADMINFAIL;
        return new DTO<Admin>(messageEnum.getCode(),messageEnum.getMessage());
    }@Override
    public DTO<Admin> getAdminByAno(String ano)  throws Exception{
        Admin admins = adminDao.getAdminByAno(ano);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(admins).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Admin>(messageEnum.getCode(),messageEnum.getMessage(),admins);
    }

    @Override
    public DTO<Admin> updateAdminByAno(Admin admin) throws Exception {
        Integer result = adminDao.updateAdminByAno(admin);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEADMINSUCCESS:MessageEnum.UPDATEADMINFAIL;
        return new DTO<Admin>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Admin> addAdmin(Admin admin) throws Exception {
        Integer result = adminDao.addAdmin(admin);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.ADDADMINSUCCESS:MessageEnum.ADDADMINFAIL;
        return new DTO<Admin>(messageEnum.getCode(),messageEnum.getMessage());
    }

    /**
     * 学生选课成功 学生成绩加1分，从选课信息中可以获得学生的学号就能查询出学生原来的成绩
     * @param
     * @return
     * @throws Exception
     */
    @Cacheable(value = "myCache",key = "#cacheId")
    @Override
    public DTO<Admin> getAdminByPage(PageInfo pageInfo ,String cacheId) throws Exception {
        List<Admin> admins = adminDao.getAdminByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(admins).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Admin>(messageEnum.getCode(),messageEnum.getMessage(),admins,pageInfo);
    }
    public DTO<Admin> getAdminByPage(PageInfo pageInfo) throws Exception {
        List<Admin> admins = adminDao.getAdminByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(admins).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Admin>(messageEnum.getCode(),messageEnum.getMessage(),admins,pageInfo);
    }

    @Override
    public DTO<Account> getAccountByAccountName(String accountName) throws Exception {
        DTO<Account> dto = new DTO<Account>();
        Optional<Account> adminOP = Optional.ofNullable(adminDao.getAccountByAccountName(accountName));
        dto.setT(adminOP.get());
        MessageEnum messageEnum = adminOP.isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        dto.setCode(messageEnum.getCode());
        dto.setMsg(messageEnum.getMessage());
        return dto;
    }
}
